package com.liangzhen.verynews.fragment;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.liangzhen.verynews.R;
import com.liangzhen.verynews.adapter.GameAdapter;
import com.liangzhen.verynews.adapter.Verynews_news_adapter;
import com.liangzhen.verynews.bean.Game;
import com.liangzhen.verynews.bean.News;
import com.liangzhen.verynews.data.ParseNewsData;
import com.liangzhen.verynews.data.ParseNewsGame;
import com.liangzhen.verynews.data.URLData;
import com.liangzhen.verynews.widget.PullToRefreshLayout;
import com.liangzhen.verynews.widget.PullToRefreshLayout.OnRefreshListener;

@SuppressLint({ "ValidFragment", "HandlerLeak" })
public class MainFragment extends Fragment implements OnRefreshListener {

	private ListView listView;
	private int tag;
	private Verynews_news_adapter verynews_news_adapter;
	private ArrayList<News> newslist;
	private static final int INIT = 0;
	private static final int REFRESH = 1;
	private static final int LOAD = 2;
	private int page = 1;
	private PullToRefreshLayout pullToRefreshManager;
	private PullToRefreshLayout pullToLoadManager;
	private GameAdapter adapter;
	private Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg) {
			if (msg.what == INIT) {
				newslist = (ArrayList<News>) msg.obj;
				verynews_news_adapter.BindData(newslist);
				//adapter.BindData(newslist);
				listView.setAdapter(verynews_news_adapter);
				verynews_news_adapter.notifyDataSetChanged();
			}
			if (msg.what == REFRESH) {
				newslist = (ArrayList<News>) msg.obj;
				verynews_news_adapter.BindData(newslist);
				listView.setAdapter(verynews_news_adapter);
				pullToRefreshManager.refreshFinish(PullToRefreshLayout.SUCCEED);
				verynews_news_adapter.notifyDataSetChanged();
			}
			if (msg.what == LOAD) {
				newslist = (ArrayList<News>) msg.obj;
				verynews_news_adapter.BindData(newslist);
				listView.setAdapter(verynews_news_adapter);
				listView.setSelection((page - 1) * 19);
				pullToLoadManager.loadmoreFinish(PullToRefreshLayout.SUCCEED);
				verynews_news_adapter.notifyDataSetChanged();
			}
		}
	};

	public MainFragment(int tag) {
		this.tag = tag;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.main_fragment, container, false);
		listView = (ListView) view.findViewById(R.id.news_listview);
		adapter = new GameAdapter(getActivity());
		verynews_news_adapter = new Verynews_news_adapter(getActivity());
		PullToRefreshLayout pullToRefreshLayout = ((PullToRefreshLayout) view
				.findViewById(R.id.refresh_view));
		pullToRefreshLayout.setOnRefreshListener(this);
		init();
		return view;
	}

	void init() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				 //String path = "http://appapi.very-news.com/moreNews.m?pull_type=down&channel=play&sort_time=0&version_name=1.2.1&start_count=1&udid=E6BA61777016BE968AEFEA9FB9A065EB&uid=";
				ParseNewsData parseNewsData = new ParseNewsData();
				//ParseNewsGame parseNewsGame = new ParseNewsGame();
				ArrayList<News> result = parseNewsData.getNewsList(URLData
						.URL(tag, 1));
				//ArrayList<Game> result = parseNewsGame.getNewsList(path);
				Message msg = Message.obtain();
				msg.obj = result;
				msg.what = INIT;
				handler.sendMessage(msg);
			}
		}).start();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
		pullToRefreshManager = pullToRefreshLayout;
		new Thread(new Runnable() {
			@Override
			public void run() {
				ParseNewsData parseNewsData = new ParseNewsData();
				ArrayList<News> result = parseNewsData.getNewsList(URLData
						.URL(tag, 1));
				if (result != null) {
					Message msg = Message.obtain();
					msg.obj = result;
					msg.what = REFRESH;
					handler.sendMessage(msg);
				}
			}
		}).start();
	}

	@Override
	public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
		pullToLoadManager = pullToRefreshLayout;
		new Thread(new Runnable() {
			@Override
			public void run() {
				ParseNewsData parseNewsData = new ParseNewsData();
				ArrayList<News> result = parseNewsData.getNewsList(URLData
						.URL(tag, ++page));
				if (result != null) {
					Message msg = Message.obtain();
					msg.obj = result;
					msg.what = LOAD;
					handler.sendMessage(msg);
				}
			}
		}).start();
	}
}
